package project.sample.io;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author: WUZH009
 * @date: 2019/8/21 10:36
 **/
public class IOHttpServer {

    public static void start() {
        InputStream in = null;
        OutputStream out = null;
        try {
            ServerSocket serverSocket = new ServerSocket(8889);
            while (true) {
                Socket socket = serverSocket.accept();
                in = socket.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                    if ("".equals(line)) {
                        break;
                    }
                }
                out = socket.getOutputStream();
                String resp = "HTTP/1.1 200\n" +
                        "Content-Type: text/html;charset=UTF-8\n" +
                        "Content-Length: 2\n" +
                        "Date: Wed, 21 Aug 2019 03:32:14 GMT\n\n" +
                        "ok\n";
                out.write(resp.getBytes());
                out.flush();
                out.write("ok\n".getBytes());
                out.flush();
//                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            /*if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }*/
        }

    }

    public static void main(String[] args) {
        start();
    }

}
